package org.ling.xuan.pannel;

import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBTextArea;
import com.intellij.ui.table.JBTable;
import org.ling.xuan.compoment.CustomTableCellEditor;
import org.ling.xuan.util.ExUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.ResultSetMetaData;

public class SelectTablePanel extends JBPanel {
    private final JBTextArea sqlTextArea;
    private final JButton executeButton;
    private final JBTable resultTable;
    private final DefaultTableModel tableModel;
    private final JBTextArea jbTextArea;
    private boolean showTable = true;

    public SelectTablePanel(JdbcTemplate jdbcTemplate) {
        setLayout(new GridLayout(4, 1));
        sqlTextArea = new JBTextArea(8, 50);
        executeButton = new JButton("Execute");
        resultTable = new JBTable();
        tableModel = new DefaultTableModel();
        resultTable.setModel(tableModel);
        resultTable.setDefaultEditor(Object.class,new CustomTableCellEditor());
        jbTextArea = new JBTextArea();
        add(new JScrollPane(sqlTextArea));
        add(executeButton);
        add(new JScrollPane(resultTable));
        add(new JScrollPane(jbTextArea));
        if (showTable) {
            if (resultTable.getRowCount() > 0) {
                jbTextArea.setVisible(false);
                resultTable.setVisible(true);
            } else {
                jbTextArea.setText("no data");
                resultTable.setVisible(false);
                jbTextArea.setVisible(true);
            }
        } else {
            jbTextArea.setText("no data");
            resultTable.setVisible(false);
            jbTextArea.setVisible(true);
        }
        executeButton.addActionListener(e -> {
            String sql = sqlTextArea.getText();
            executeSQL(jdbcTemplate, sql);
            parentRepaint();
        });
    }

    private void parentRepaint() {
        repaint();
    }

    private void executeSQL(JdbcTemplate jdbcTemplate, String sql) {
        tableModel.setRowCount(0);
        tableModel.setColumnCount(0);
        if (sql.trim().toLowerCase().startsWith("select")) {
            try {
                jdbcTemplate.query(sql, (RowMapper<Object>) (rs, rowNum) -> {
                    if (tableModel.getColumnCount() == 0) {
                        ResultSetMetaData metaData = rs.getMetaData();
                        int columnCount = metaData.getColumnCount();
                        for (int i = 1; i <= columnCount; i++) {
                            tableModel.addColumn(metaData.getColumnName(i));
                        }
                    }
                    Object[] rowData = new Object[tableModel.getColumnCount()];
                    for (int i = 1; i <= tableModel.getColumnCount(); i++) {
                        rowData[i - 1] = rs.getObject(i);
                    }
                    tableModel.addRow(rowData);
                    return 1;
                });
                if (tableModel.getRowCount() > 0) {
                    showTable = true;
                } else {
                    jbTextArea.setText("no selectData");
                }
            } catch (Exception e) {
                showTable = false;
                String ss = ExUtil.getExMsg(e, "\n");
                jbTextArea.setText(ss);
            }
        } else {
            showTable = false;
            try {
                int i = jdbcTemplate.update(sql);
                jbTextArea.setText("modify rows:" + i);
            } catch (Exception e) {
                String ss = ExUtil.getExMsg(e, "\n");
                jbTextArea.setText(ss);
            }
        }
        if (showTable) {
            jbTextArea.setVisible(false);
            resultTable.setVisible(true);
        } else {
            resultTable.setVisible(false);
            jbTextArea.setVisible(true);
        }
    }
}
